package me.panpf.sketch;

import me.panpf.sketch.drawable.DrawableCallback;
import me.panpf.sketch.drawable.MiPixmapElement;
import me.panpf.sketch.zoom.PixmapBuild;
import ohos.agp.components.element.PixelMapElement;
import ohos.global.resource.Resource;
import ohos.media.image.PixelMap;

public class PixelMapDrawable extends MiPixmapElement {
    protected boolean hasInvalidateTask = false;
    private DrawableCallback callback;

    public PixelMapDrawable(PixelMap pixelMap) {
        super(new PixmapBuild(null).setPixelMap(pixelMap));
    }
    public PixelMapDrawable(PixmapBuild pixelMap) {
        super(pixelMap);
    }

    public PixelMapDrawable(Resource resource) {
        super(resource);
    }

    public PixelMapDrawable(Resource resource, boolean startDecode) {
        super(resource, startDecode);
    }

    public DrawableCallback getDrawableCallback() {
        return callback;
    }

    public void setDrawableCallback(DrawableCallback callback) {
        this.callback = callback;
        if (hasInvalidateTask) {
            invalidateSelf();
            hasInvalidateTask = false;
        }
    }

    public void invalidateSelf() {
        final DrawableCallback callback = getDrawableCallback();
        if (callback != null) {
            callback.invalidateDrawable(this);
        } else {
            hasInvalidateTask = true;
        }
    }
}
